<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/18
 * Time: 22:59
 */
namespace App\Lib\Sdk\Spider;


use App\Lib\Sdk\Spider\Service\Spider;
use Illuminate\Filesystem\Cache;
use Illuminate\Support\Facades\Storage;

class JdSpider extends BaseSpider implements Spider
{

    public function search(array $jdConfig, $itemId = null)
    {
        $api = JdApi::search;
        $params = array_merge($jdConfig, [
            'pageSize' => 1000
        ]);
        if (isset($params['cid']) && !empty($params['cid'])) {
            foreach(explode(",", $params['cid']) as $k => $item) {
                $params["cid" . ($k+1)] = $item;
            }
        }
        if ($itemId !== null) {
            $params["skuIds"] = is_array($itemId) ? implode(",", $itemId) : $itemId;
        }
        $data = $this->client->get($api, $params);
        if ($data->error  > 0) {
            $this->err = $data->msg;
            return false;
        }
        $res = $data->data->toArray();
        $response = [];
        foreach ($res as $item) {
            if (!empty($item['couponInfo']['couponList'])) {
                $coupon = current($item['couponInfo']['couponList']);
                if ($item['priceInfo']['price'] < $coupon['quota']) {
                    continue;
                }
                $item['shortURL'] = $this->getCouponLink($item['materialUrl'],  $coupon);
                $item['couponInfo'] = $coupon;
                array_push($response, $item);
            }
            if (count($response) >= 50) {
                break;
            }
        }
        return $response;
    }


    public function getCouponLink($url, $coupon)
    {
        $config = $this->config;
        $api = JdApi::prom;
        $params = [
            'unionId' => $config['unionid'],
            'materialId' => $url,
            'couponUrl' =>  $coupon['link']
        ];
        $res = $this->client->get($api, $params);
        return $res->data->shortURL;
    }
    //京东活动
  /*  public function getCouponActive()
    {
        $cache = Storage::disk("public");
        $file = "jd_" . date("Ymd");
        if (!$cache->exists($file)) {
            $api = JdApi::activity;
            $config = $this->config;
            $params = [
                'unionId' => $config['unionid'],
            ];
            $res = $this->client->get($api, $params);
            $data = $res->data->toArray();
            $cache->put($file, $data);
        }
    }*/

    public static function getJdProduct(array $data)
    {
        $model = new self();
        $model->title = $data['skuName'];
        $model->shortTitle = $model->title;
        $model->couponPrice =$data['priceInfo']['lowestCouponPrice'];
        $model->originPrice = $data['priceInfo']['price'];
        $model->couponDiscount = $data['couponInfo']['discount'];   //优惠券价格,折扣价格
        $model->link = $data['shortURL'];               //商品短链接
        $model->couponLink = $data['couponInfo']['link'];
        $model->sales  = 0;
        $model-> description =  $model->title;
        $model->cover = "<img src='" .current($data['imageInfo']['imageList'])['url'] . "'>";
        $model->shop = "京东";  //淘宝，天猫，京东，拼多多
        $model->rate = "";
        $model->skuId = $data['skuId'];
        $model->couponExpire = date("Y-m-d H:i:s", $data['couponInfo']['useEndTime'] / 1000);
        return
}